package com.Controller;

import com.pojo.Srd;
import com.service.srd.SrdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping(value = "/srd")
public class SrdController {
    @Autowired
    private SrdService srdService;

    @RequestMapping(value = "/list")
    public String list(Model model,@RequestParam(required = false) String goodsName,@RequestParam(defaultValue = "1") int pageNo){
        int pageSize = 5;
        int pageIndex = (pageNo - 1) * pageSize;
        List<Srd> list = srdService.list(pageIndex,pageSize,goodsName);
        int pageCount = srdService.count();
        model.addAttribute("list",list);
        model.addAttribute("pageNo",pageNo);
        model.addAttribute("pageCount",pageCount);
        return "srd/list";
    }

    @RequestMapping(value = "/toAdd")
    public String toAdd(){
        return "srd/add";
    }

    @RequestMapping(value = "/add")
    public String add(Srd srd){
        int result = srdService.add(srd);
        if (result > 0){
            return "redirect:/srd/list";
        }
        return "srd/toAdd";
    }

    @RequestMapping(value = "/selectId")
    public String selectId(Model model,int id){
        Srd srd = srdService.selectId(id);
        model.addAttribute("srd",srd);
        return "srd/selectId";
    }

    @RequestMapping(value = "/delete")
    public String delete(int id){
        int result = srdService.delete(id);
        return "redirect:/srd/list";
    }
}
